<?php
class Products_model extends CI_Model {

    var $code = '';
    var $description = '';
    var $group_id = '';
    var $insert_date = '';
    var $status = '';
    var $picture = '';
    var $cost = '';
    var $price = '';
    var $unit_desc = '';
    var $pack_desc = '';
    var $pack_units = '';
    var $box_packs = '';
    private $_tabla = 'products';

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    public function find2($code)
    {
        $query = $this->db->where('products_id', $code)->get($this->_tabla);
        $rs = $query->result();
        if (isset($rs[0])) {
            return $rs[0];
        } 
        return false;
    }
    
    public function find($code)
    {
        $query = $this->db->where('code', $code)->get($this->_tabla);
        $rs = $query->result();
        if (isset($rs[0])) {
            return $rs[0];
        } 
        return false;
    }
      
    public function getAll()
    {
        $query = $this->db;
        $query = $query->where('status', 1);
        $query = $this->db->get($this->_tabla);
        return $query->result();
    }
    
   
    public function search($code = null, $description = null, $category = null)
    {
        $query = $this->db;
        if (isset($code) && $code != '') {
            $query = $query->like($this->_tabla.'.code', $code, 'both');
        }
        if (isset($description) && $description != '') {
            $query = $query->like($this->_tabla.'.description', $description);
        }
        if (isset($category) && $category != '') {
            $query = $query->join('product_groups', "product_groups.id = ".$this->_tabla.".group_id", 'left');
            $query = $query->like('product_groups.description', $category);
        }
//        $query = $query->where($this->_tabla.'.status', 1);
        $query = $query->get($this->_tabla);
        $rs = $query->result();
        return $rs;
     }
     
     
     public function getcode(){
        $query = $this->db->query("select products_id from products where status = '1'");
        $rs = $query->result();
        return $rs;
     }
     
     public function getnameproducts(){
        $query = $this->db->query("select description from products where status = '1'");
        $rs = $query->result();
        return $rs;
     }
    
    public function productsxwarehouses($code){
        
        $query = $this->db->query("SELECT wp.id_warehouse, w.name, wp.Units, wp.boxes, wp.packs FROM warehouses w, warehouses_product wp, products p WHERE wp.id_product='".$code."' and w.id = wp.id_warehouse and p.code = wp.id_product");
        $rs = $query->result();
        return $rs;
        
    }
                
    public function add($data)
    {
        $this->db->insert($this->_tabla, $data);
        return true;
    }
    
    public function delete($code)
    {
        $this->db->where('products_id', $code);
        $this->db->update($this->_tabla, array('status' => 0));
        return true; 
    }

    public function deleteGroup($code)
    {
        $this->db->where('group_id', $code);
        $this->db->update($this->_tabla, array('status' => 0));
        return true; 
    }
    
    public function update($code, $data)
    {
        if ($data['products_id'] == false) {
            unset($data['products_id']);
        }
        if ($data['insert_date'] == false) {
            unset($data['insert_date']);
        }

        $this->db->where('products_id', $code);
        $this->db->update($this->_tabla, $data);
        return true;
    }
}